<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/4
 * Time: 15:33
 */

namespace app\services\commands;


use app\models\ModBaseInfo;
use core\base\BaseService;

class InitRecruitService extends BaseService
{

    public function getExcelPath()
    {
        $staticPath = \Yii::getAlias('@app'). '/web/data/excel';
        return $staticPath;
    }

    public function getExcelData()
    {
        $excelPath = $this->getExcelPath(). '/init.xlsx';
        $reader = new \PHPExcel_Reader_Excel2007();
        $data = $reader->load($excelPath);
        $sheet = $data->getActiveSheet();
        $data = $sheet->toArray();
        $result = [];
        foreach ($data as $value) {
            $tmp = [];
            $tmp['resume_id'] = $value[0];
            $tmp['name'] = $value[3];
            $tmp['mobile'] = $value[4];
            $tmp['email'] = $value[5];
            $tmp['file_name'] = $value[1];
            $tmp['real_file_name'] = $value[2];
            $result[] = $tmp;
        }
        return $result;
    }

    public function saveData()
    {
        $data = $this->getExcelData();
        ModBaseInfo::batchInsert($data);
    }

    public function init()
    {
        $this->saveData();
    }

    public function initCheckStatus()
    {
        $resumeIDs = $this->getCheckData();
        // 更新为已检查
        ModBaseInfo::updateAll(['is_checked' => 1], ['resume_id' => $resumeIDs]);
        echo 'success...';
    }

    public function getCheckData()
    {
        $path = $this->getExcelPath(). '/checked.xlsx';
        $reader = new \PHPExcel_Reader_Excel2007();
        $data = $reader->load($path);
        $sheet = $data->getActiveSheet();
        $data = $sheet->toArray();
        $resumeIDs = [];
        for ($i = 0; $i < count($data); $i ++) {
            $resumeIDs[] = $data[$i][0];
        }
        return $resumeIDs;
    }
}